﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZHDJ.Data.Models.Edu;

namespace SysManage.Module.Edu
{
    public partial class test : System.Web.UI.Page
    {
        /// <summary>
        /// 字母数组
        /// </summary>
        public string[] _leter = new string[] { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L" };
        /// <summary>
        /// 测试题数
        /// </summary>
        public const int _testCount = 3;

        /// <summary>
        /// 题目选项（做题前）
        /// </summary>
        private DataSet _TestSubject;
        /// <summary>
        /// 题目选项（做题前）
        /// </summary>
        public DataSet TestSubject
        {
            get
            {
                if (_TestSubject == null)
                {
                    _TestSubject = PartyEduSubject.GetPartyEduSubjectTest(_testCount,  "51d09bcd37264854aa3e3cf2276d8c61");
                }
                return _TestSubject;
            }
        }

        /// <summary>
        /// 题目选项（做题后）
        /// </summary>
        private DataSet _TestSubject2;
        /// <summary>
        /// 题目选项（做题后）
        /// </summary>
        public DataSet TestSubject2
        {
            get
            {
                return _TestSubject2;
            }
            set {
                _TestSubject2 = value;
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadData();
            }
        }

        public void LoadData()
        {
            //加载题目
            var dv = TestSubject.Tables[0].AsDataView();
            dv.Sort="ID";
            this.rptSubject.DataSource = dv;
            this.rptSubject.DataBind();
            foreach (DataRowView item in dv)
            {
                this.hdSubjectIDs.Value += Convert.ToString(item["ID"]) + ",";
            }
        }

        /// <summary>
        /// 绑定选项
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void rptSubject_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.SelectedItem)
            {
                //加载选项
                var data = e.Item.DataItem;
                Repeater Repeater = e.Item.FindControl("rptOptions") as Repeater;
                HiddenField hiddenField = e.Item.FindControl("hdSubjectID") as HiddenField;

                if (Repeater == null)
                {
                    return;
                }
                if (IsPostBack)
                {
                    //答题后
                    var dataview = TestSubject2.Tables[1].AsDataView();
                    dataview.RowFilter = string.Format("SubjectID='{0}'", hiddenField.Value);
                    Repeater.DataSource = dataview;
                    Repeater.DataBind();
                }
                else
                {
                    //答题前
                    var dataview = TestSubject.Tables[1].AsDataView();
                    dataview.RowFilter = string.Format("SubjectID='{0}'", hiddenField.Value);
                    Repeater.DataSource = dataview;
                    Repeater.DataBind();
                }
                
            }
        }

        /// <summary>
        /// 提交测试
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmitEx_Click(object sender, EventArgs e)
        {
            
            var subjectIDs = hdSubjectIDs.Value.Trim(',').Split(',');
            if (subjectIDs.Length != _testCount)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "tip", "dataError();", true);
                return;
            }
            //加载数据
            List<TestSubject> test = new List<Edu.TestSubject>();
            TestSubject2 = PartyEduSubject.GetPartyEduSubjectTest(subjectIDs);
            foreach (var item in subjectIDs)
            {
                TestSubject subject = new TestSubject();
                subject.SubjectID = item;
                var dataview = TestSubject2.Tables[1].AsDataView();
                dataview.RowFilter = string.Format("SubjectID='{0}'", item);
                foreach (DataRowView op in dataview)
                {
                    // 题目答案选项ID
                    if (Convert.ToBoolean(op["IsAnswer"]))
                    {
                        subject.Answer.Add(Convert.ToString(op["ID"]));
                        hdAnswer.Value += Convert.ToString(op["ID"]) + ",";
                    }
                    //用户提交的答案选项ID
                    if (!string.IsNullOrEmpty(Request.Form[Convert.ToString(op["ID"])]))
                    {
                        subject.Submit.Add(Convert.ToString(op["ID"]));
                        hdSubmit.Value += Convert.ToString(op["ID"]) + ",";
                    }
                    
                }
                dataview.RowFilter = "";
                test.Add(subject);
            }
            //验证答案
            foreach (var item in test)
            {
                if (item.Answer.Count != item.Submit.Count) {
                    item.IsRight = false;
                    break;
                }
                //交集
                var rs = item.Answer.Except(item.Submit);
                item.IsRight = !rs.Any();
            }

            hdAnswer.Value = hdAnswer.Value.Trim(',');
            hdSubmit.Value = hdSubmit.Value.Trim(',');
            divbtnsubmit.Visible = false;//隐藏提交按钮
            litRs.Text = test.Count(a => a.IsRight)==_testCount?"全对了":"没有全对";
            var dv = TestSubject2.Tables[0].AsDataView();
            dv.Sort = "ID";
            this.rptSubject.DataSource = dv;
            this.rptSubject.DataBind();

        }
    }

    public class TestSubject
    {
        public TestSubject()
        {
            Answer = new List<string>();
            Submit = new List<string>();
        }

        /// <summary>
        /// 题目ID
        /// </summary>
        public string SubjectID { get; set; }
        /// <summary>
        /// 题目答案选项ID
        /// </summary>
        public List<string> Answer { get; set; }

        /// <summary>
        /// 用户提交的答案选项ID
        /// </summary>
        public List<string> Submit { get; set; }

        /// <summary>
        /// 是否做对
        /// </summary>
        public bool IsRight { get; set; }

    }
}
